<?php

namespace app\index\controller;

use think\Controller;
use think\Session;

class Base extends Controller
{
    protected $uid      = 0;        // 用户id（session缓存）
    protected $username = '游客';   // 用户名称
    protected $photo    = '';       // 用户头像
    protected $limit    = 0;        // 默认查询数据个数
    protected $admin    = 0;        // 是否为管理员
    protected $balance  = 0;        // 用户余额
    protected $identity = 0;        // 用户级别


    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        Session::get('uid')	        == null || $this->uid	    = Session::get('uid');
        Session::get('photo')       == null || $this->photo     = Session::get('photo');
        Session::get('username')    == null || $this->username  = Session::get('username');


        // 验证是否授权登录中
        if($this->uid<1){

            // 跳转授权页面
            $this->redirect(url('index/wauth/getRequest'));				// 正式环境
            // $this->redirect(url('index/wauth/login'));						// 生产环境
        }

        // 用户信息
        $user_info = Db("users")->where('id',$this->uid)->find();
        
        $this->admin    = $user_info['is_manager'];
        $this->identity = $user_info['identity'];
        $this->balance  = $user_info['balance'];

        // 后台配置信息
        $web_config = Db("web_config")->field('title,config,value')->select();
        foreach ($web_config as $key => $value) {
            $web_con[$value['config']] = $value['value'];
        }
        $this->assign('web_config',$web_con);

        // 微信jdk文件配置
        $this->getWxConfig();
    }

    /**
     * 微信配置信息
     * return       timestamp           时间戳
     *              noncestr            生成签名的随机串
     *              signature           签名
     *              appId               appid
     */
    private function getWxConfig()
    {
        $weixin = new Weixin();

        $url = request()->domain() . $this->request->url();
        $js_config = $weixin->js_config($url);

        $share_icon = request()->domain() . '/gupiao/images/share_icon.jpg';

        $this->assign('share_icon',$share_icon);
        $this->assign('web_url',$url);
        $this->assign('jsconfig', $js_config);
    }



}
